import java.util.LinkedList;
import java.util.Queue;

class Solution130 {
    private int[] dx = { 0, 0, 1, -1 };
    private int[] dy = { 1, -1, 0, 0 };
    private int m, n;
    public void solve(char[][] board) {
        m = board.length ; 
        n = board[0].length ;
        for(int i=0 ; i < n ; i ++){
            if(board[0][i] == 'O') bfs(board , 0 , i) ;
            if(board[m-1][i] == 'O') bfs(board , m-1 , i) ; 
        }
        for(int i=0 ; i < m ; i ++){
            if(board[i][0] == 'O') bfs(board , i , 0) ; 
            if(board[i][n-1] == 'O') bfs(board , i , n-1) ;
        }
        
        for(int i=0 ; i < m ; i ++){
            for(int j= 0 ; j < n ; j ++){
                if(board[i][j] == 'O'){
                    board[i][j] = 'X';
                }else if(board[i][j] == '*'){
                    board[i][j] = 'O';
                }
            }
        }

    }
    private void bfs (char[][] board , int x , int y){
       Queue<int[]> queue = new LinkedList<>() ;
       queue.add(new int[] {x , y}) ;
       board[x][y] = '*' ; 
       while(!queue.isEmpty()){
         int[] arr = queue.poll() ;
         int a = arr[0] , b = arr[1] ; 
         for(int i=0 ; i < 4 ; i ++){
            int c = a+dx[i] , d = b+dy[i]  ; 
            if(c >= 0 && c < m && d >=0 && d < n && board[c][d] == 'O'){
                board[c][d] = '*' ; 
                queue.add(new int[] {c , d}) ; 
            }
         }

       }
    }
}